/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 10, 2011 12:12:38 PM
 */
package carrello.model.entities;

import carrello.model.association.AttributeValue;
import carrello.util.EntityManagerUtil;
import java.util.Set;
import javax.persistence.*;

@NamedQueries({
	@NamedQuery(
	    name = "getAttributeByName",
	    query = "from Attribute at where at.name = :aname"
	    )
        })
@Entity
@Table(name = "Attribute")
public class Attribute {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "attribute_id")
    private int attributeId;
    private String name;
    private String type;
    private char required;
    
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "CategoryHasAttribute" ,
        joinColumns = { @JoinColumn(name = "attribute_id")}, 
        inverseJoinColumns = { @JoinColumn(name = "category_id")})
    private Set<Category> category;
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pkey.attribute")
    private Set<AttributeValue> attributeValue;

       
    public Attribute() {}
    public Attribute(String name, String type, char required)
    {
        this.name        = name;
        this.type        = type;
        this.required    = required;
    }

    public Attribute(String name, String type, char required, Set<Category> category) {
        this.name = name;
        this.type = type;
        this.required = required;
        this.category = category;
    }

    public int getAttributeId() {
        return attributeId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getRequired() {
        return required;
    }

    public void setRequired(char required) {
        this.required = required;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Set<Category> getCategory() {
        return category;
    }

    public void setCategory(Set<Category> category) {
        this.category = category;
    }

    public Set<AttributeValue> getAttributeValue() {
        return attributeValue;
    }

    public void setAttributeValue(Set<AttributeValue> attributeValue) {
        this.attributeValue = attributeValue;
    }
    
    public static Attribute getAttributeByName(String name)
    {
       
       Object i = EntityManagerUtil.getEntityManager().getQuery("getAttributeByName")
                                  .setParameter("aname", name).uniqueResult();
                                  
       if ( i instanceof Attribute) {
           return (Attribute)i;
       }
       return null;
    }
    
//    public static void main(String [] args)
//    {
//        
//        Attribute cat = getAttributeByName("Weight");
//        System.out.println(cat.getType());
//    }
}
